package com.sekill.order.service.impl;

import cn.hutool.core.lang.Console;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.sekill.order.config.MapUtil;
import com.sekill.order.config.SignatureUtil;
import com.sekill.order.service.PayService;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class PaySeviceImpl implements PayService {

    @Value("${pay.mchId}")
    private String mchId;

    @Value("${pay.mchKey}")
    private String mchKey;

    //调用第三方的下单接口
    @Override
    public String underOrder(String addorder, String totalPay, String note) throws JSONException {

        //签名
        Map<String,String> paramsMap = new HashMap();
        paramsMap.put("nonceStr",System.currentTimeMillis()+"");
        paramsMap.put("mchId",mchId);
        paramsMap.put("out_trade_no",addorder);
        paramsMap.put("total_fee",totalPay);
        paramsMap.put("notify_url","http://localhost:9503/pay/notify");
        paramsMap.put("note",note);
        paramsMap.put("payType",2+"");
        paramsMap.put("sourceType",0+"");
        paramsMap.put("sign_type","md5");

        String sign = SignatureUtil.generateSign(paramsMap,"md5",mchKey);
        paramsMap.put("sign",sign);

        String s = HttpRequest.post("http://localhost:8099/order/unifiedorder")
                .header(Header.CONTENT_TYPE, "application/json")//头信息，多个头信息多次调用此方法即可
                .header("Accept","*/*")
                .body(JSON.toJSONString(paramsMap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        Console.log(s);

        JSONObject jsonObject = new JSONObject(s);
        Object prepayId = jsonObject.get("prepayId");

        return prepayId.toString();
    }

    @Override
    public String payInfo(String orderId) throws JSONException {

        Map<String,String> map=new HashMap();
        map.put("mchId",mchId);
        map.put("nonceStr",System.currentTimeMillis()+"");
        map.put("outTradeNo",orderId);

        String sign = SignatureUtil.generateSign(map, null, mchKey);
        map.put("sign",sign);

        String key = HttpRequest.post("http://localhost:8099/pay/getPayInfo")
                .header(Header.CONTENT_TYPE, "application/json")
                .header("Accept", "*/*")
                .body(JSON.toJSONString(map))
                .timeout(20000)
                .execute().body();
        Console.log(key);
        //获取支付的code
        JSONObject jsonObject = new JSONObject(key);
        Object data = jsonObject.get("data");
        JSONObject code = new JSONObject(data.toString());
        Object payUrl = code.get("payUrl");
        return payUrl.toString().substring(27);
    }

    @Override
    public boolean pay(String s) throws JSONException {
        Map<String,String> map=new HashMap();
        map.put("paykey",s);

        String key = HttpRequest.post("http://localhost:8099/pay/"+s)
                .header(Header.CONTENT_TYPE, "application/json")
                .header("Accept", "*/*")
                .body(JSON.toJSONString(map))
                .timeout(20000)
                .execute().body();
        Console.log(key);

        JSONObject jsonObject = new JSONObject(key);
        Object data = jsonObject.get("data");
        if ("sucess".equals(data)){
            return true;
        }
        return false;
    }


}
